#!/bin/bash
###SHELLPACK preamble thpfioscale-bench 2.99
###SHELLPACK addon fragment

MADV_HUGEPAGE=0

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam	--min-threads	THPFIOSCALE_MIN_THREADS
###SHELLPACK parseargParam	--max-threads	THPFIOSCALE_MAX_THREADS
###SHELLPACK parseargParam	--fio-threads	THPFIOSCALE_FIO_THREADS
###SHELLPACK parseargParam	--thp-size	THPFIOSCALE_THP_WSETSIZE
###SHELLPACK parseargParam	--fio-size	THPFIOSCALE_FIO_WSETSIZE
###SHELLPACK parseargParam	--madv-huge	THPFIOSCALE_MADV_HUGEPAGE
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_external_install_required fio fio-${VERSION} ${VERSION}
###SHELLPACK check_install_required thpfioscale-${VERSION}
###SHELLPACK init_complete

if [ "$THPFIOSCALE_MADV_HUGEPAGE" = "yes" ]; then
	MADV_HUGEPAGE=1
fi

ulimit -n 1048576
fragment prepare --method fio-random-reader --fio-size $THPFIOSCALE_FIO_WSETSIZE --fio-threads $THPFIOSCALE_FIO_THREADS --fio-version $VERSION
if [ $? -ne 0 ]; then
	die "fragment prepare --method fio-random-reader failed"
fi
fragment run     --method fio-random-reader --fio-size $THPFIOSCALE_FIO_WSETSIZE --fio-threads $THPFIOSCALE_FIO_THREADS --fio-version $VERSION
if [ $? -ne 0 ]; then
	die "fragment run --method fio-random-reader failed"
fi

echo Warmup complete
###SHELLPACK threads_large_stride_begin $THPFIOSCALE_MIN_THREADS $THPFIOSCALE_MAX_THREADS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	echo Starting thpfioscale $NR_THREADS/$THPFIOSCALE_MAX_THREADS size:$THPFIOSCALE_THP_WSETSIZE madv:$MADV_HUGEPAGE
	$TIME_CMD -o $LOGDIR_RESULTS/threads-${NR_THREADS}.time \
		./thpfioscale $NR_THREADS $THPFIOSCALE_THP_WSETSIZE $MADV_HUGEPAGE \
			> $LOGDIR_RESULTS/threads-${NR_THREADS}.log 2>&1
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
	gzip $LOGDIR_RESULTS/threads-${NR_THREADS}.log
###SHELLPACK threads_stride_end
echo Finished thpfioscale

fragment cleanup --method fio-random-reader

exit $SHELLPACK_SUCCESS
